//
//  ZJSSwitchControl.m
//  ZJS
//
//  Created by qingqiu.yuan on 16/9/12.
//  Copyright © 2016年 zall. All rights reserved.
//

#import "ZJSSwitchControl.h"
#import "UIView+ZallCornerRadius.h"
#import "ZallBorderView.h"

@interface ZJSSwitchControl()
{
    
}

@property (nonatomic,strong) UIButton *leftButton;//左侧按钮
@property (nonatomic,strong) UIButton *rightButton;//右侧按钮
@property (nonatomic,strong) UIButton *thumbView;//滑块
@property (nonatomic,strong) NSString *leftTitles;//左标题
@property (nonatomic,strong) NSString *rightTitles;//右标题
@property (nonatomic, assign) ZJSSwitchDirection currentDirection;

@end

@implementation ZJSSwitchControl

- (id)initWithFrame:(CGRect)frame leftTtitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle
{
    self = [super initWithFrame:frame];
    if (self) {
        _leftTitles = leftTitle;
        _rightTitles = rightTitle;
        [self setLeftAndRightButton];
        self.backgroundColor = [UIColor whiteColor];
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 4;
        self.layer.borderColor = UIColorFromHexRGB(0xff6600).CGColor;
        self.layer.borderWidth = 1.0;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.scrolViewColor) {
        self.thumbView.backgroundColor = self.scrolViewColor;
    }
    self.thumbView.width = CGRectGetWidth(self.frame) / 2;
    self.thumbView.height = CGRectGetHeight(self.frame) - 2;
    self.thumbView.top = 1.0f;
}

- (ZJSSwitchDirection)getCurrentDirection
{
    return self.currentDirection;
}

- (void)setLeftAndRightButton
{
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    [self addSubview:self.thumbView];
    
    [self.rightButton setTitle:_rightTitles forState:UIControlStateNormal];
    [self.leftButton setTitle:_leftTitles forState:UIControlStateNormal];
    
    [self.leftButton setTitleColor:UIColorFromHexRGB(0xff6600) forState:UIControlStateNormal];
    [self.rightButton setTitleColor:UIColorFromHexRGB(0xff6600) forState:UIControlStateNormal];
    
    [self.thumbView setTitle:_leftTitles forState:UIControlStateNormal];
    [self.thumbView setTitleColor:UIColorFromHexRGB(0xffffff) forState:UIControlStateNormal];
    
    //取消闪烁
    self.thumbView.adjustsImageWhenHighlighted = NO;
    self.rightButton.adjustsImageWhenHighlighted = NO;
    self.leftButton.adjustsImageWhenHighlighted = NO;
}

- (void)setSelectedSwitch:(ZJSSwitchDirection)direction
{
    if (direction == ZJSSwitchLeft)
    {
        [self leftButton:nil];
    }else if (direction == ZJSSwitchRight)
    {
        [self rightButton:nil];
    }
}

- (void)leftButton:(UIButton *)sender
{
    //向左
    self.currentDirection = ZJSSwitchLeft;
    [self.thumbView setTitle:_leftTitles != nil ? _leftTitles : self.leftButton.titleLabel.text forState:UIControlStateNormal];
//    [self.thumbView setCornerRadius:2 inCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft];
    [UIView animateWithDuration:0.25 animations:^{
        self.thumbView.frame = CGRectMake(0, 1, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) - 2);
        
    } completion:^(BOOL finished) {
        
    }];
    if (!sender) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(switchState:direction:)])
    {
        [self.delegate switchState:self direction:ZJSSwitchLeft];
    }
    
}

- (void)rightButton:(UIButton *)sender
{
    self.currentDirection = ZJSSwitchRight;
    //向右
    [self.thumbView setTitle:_rightTitles != nil?_rightTitles:self.rightButton.titleLabel.text forState:UIControlStateNormal];
//    [self.thumbView setCornerRadius:2 inCorners:UIRectCornerTopRight | UIRectCornerBottomRight];
    [UIView animateWithDuration:0.25 animations:^{
        self.thumbView.frame = CGRectMake(CGRectGetWidth(self.frame) / 2, 1, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) - 2);
        
    } completion:^(BOOL finished) {
        
    }];
    if (!sender) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(switchState:direction:)])
    {
        [self.delegate switchState:self direction:ZJSSwitchRight];
    }
}

#pragma mark -- setter/getter
- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _leftButton.frame = CGRectMake(0, 1, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) - 2);
        [_leftButton setCornerRadius:1 inCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft];
        [_leftButton addTarget:self action:@selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton)
    {
        _rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _rightButton.frame = CGRectMake(CGRectGetWidth(self.frame) / 2, 1, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) - 2);
        [_rightButton setCornerRadius:1 inCorners:UIRectCornerTopRight | UIRectCornerBottomRight ];
        [_rightButton addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (UIButton *)thumbView
{
    if (!_thumbView)
    {
        _thumbView = [UIButton buttonWithType:UIButtonTypeSystem];
        //默认滑块放在左侧
        _thumbView.frame = CGRectMake(0, 1, CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) - 2);
//        [_thumbView setCornerRadius:1 inCorners:UIRectCornerBottomLeft|UIRectCornerTopLeft];
        _thumbView.backgroundColor = UIColorFromHexRGB(0xff6600);
    }
    return _thumbView;
}

/**
 *  @brief  添加滑动效果 by lizhiquan  2017年05月23日14:46:38
 */
- (void)setSelectedItemIndex:(NSInteger)index animated:(BOOL)animated {
    [self willChangeValueForKey:@"currentDirection"];
    self.currentDirection = index;
    [self didChangeValueForKey:@"currentDirection"];
}

- (void)setCurrentOffset:(CGPoint)offset animated:(BOOL)animated {
    self.thumbView.hidden = NO;
    [self bringSubviewToFront:self.thumbView];
    NSInteger cur = (NSInteger)floorf(offset.x);
    [UIView animateWithDuration:(animated ? 0.25f : 0) animations:^ {
        self.thumbView.left = cur;
    }];
    if (cur >= 0 && cur < self.thumbView.width / 2) {
        [self.thumbView setTitle:_leftTitles forState:UIControlStateNormal];
        [self.thumbView setTitleColor:UIColorFromHexRGB(0xffffff) forState:UIControlStateNormal];
    }else {
        [self.thumbView setTitle:_rightTitles forState:UIControlStateNormal];
        [self.thumbView setTitleColor:UIColorFromHexRGB(0xffffff) forState:UIControlStateNormal];
    }
}

@end
